package hanhns.test.listview;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import hanhns.test.R;
import hanhns.test.provider_database.BNoteProvider;

public class CusTomSimpleCurSorAdapter extends SimpleCursorAdapter {
	private static final String TAG = "CusTomSimpleCurSorAdapter";
	
	private Cursor mCursor;
	private int mTitleId; //chi so cot cua title.
	private int mContentId; //chi so cot cua content.
	private int mId; //Chi so cot cua cot ID.
	private Context mContext;

	public CusTomSimpleCurSorAdapter(Context context, int layout, Cursor c,
			String[] from, int[] to) {
		super(context, layout, c, from, to);
		// TODO Auto-generated constructor stub
		this.mCursor = c;
		this.mContext = context;
		getColumIndices(mCursor);
		
		
	}
	
	
	private void getColumIndices(Cursor cursor) {
	     if(cursor != null) {
	    	 mTitleId = cursor.getColumnIndexOrThrow(BNoteProvider.NOTE_TITLE_COLUMN);
	         mContentId = cursor.getColumnIndexOrThrow(BNoteProvider.NOTE_CONTENT_COLUMN);
	         mId = cursor.getColumnIndex(BNoteProvider._ID_COLUMN);
	     }
	}
	
	static class ViewHolder{
		TextView title;
		TextView content;
		TextView id;
		ImageView icon;
		CheckBox check;	
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		Log.e(TAG, "call newView");
		// TODO Auto-generated method stub
		//return super.newView(context, cursor, parent);
		View v = super.newView(context, cursor, parent);
		ViewHolder holder = new ViewHolder();
		holder.title = (TextView) v.findViewById(R.id.titleNote);
		holder.content = (TextView) v.findViewById(R.id.contentNote);
        holder.id = (TextView) v.findViewById(R.id.idNote);
        holder.icon = (ImageView) v.findViewById(R.id.icon);
        holder.check = (CheckBox) v.findViewById(R.id.noteCheck);
        
//        holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//			
//			@Override
//			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//				// TODO Auto-generated method stub
////				ContentValues value = new ContentValues();
////				String id = holder.id.getText().toString();
//				View v = (View) buttonView.getParent();
//				TextView id_textview = (TextView) v.findViewById(R.id.idNote);
//				String id = id_textview.getText().toString();
////				Uri noteUri = Uri.withAppendedPath(BNoteProvider.CONTENT_URI, id);
////				value.put(BNoteProvider.NOTE_CHECK, isChecked);
////				mContext.getContentResolver().update(noteUri, value, null, null);
//				Integer indext = new Integer(Integer.parseInt(id));
//				Toast.makeText(mContext, "id of select: " + id, Toast.LENGTH_SHORT).show();
//				if ( buttonView.isChecked() ) {
//					Toast.makeText(mContext, "checked", Toast.LENGTH_SHORT).show();
//					ListNoteUseSimpleCursorAdapter.arrayChecked.add(indext);
//					Toast.makeText(mContext, "size of array: " + ListNoteUseSimpleCursorAdapter.arrayChecked.size(), Toast.LENGTH_SHORT).show();
//				} else {
//					Toast.makeText(mContext, "unchecked", Toast.LENGTH_SHORT).show();
//					ListNoteUseSimpleCursorAdapter.arrayChecked.remove(indext);
//					Toast.makeText(mContext, "size of array: " + ListNoteUseSimpleCursorAdapter.arrayChecked.size(), Toast.LENGTH_SHORT).show();
//				}
//				ListNoteUseSimpleCursorAdapter.havecheck();
//			}
//		});
        v.setTag(holder);
		return v;
	}
	
	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// TODO Auto-generated method stub
		Log.e(TAG, "call bindView");
		ViewHolder holder = (ViewHolder) view.getTag();
		holder.title.setText(cursor.getString(mTitleId));
		holder.content.setText(cursor.getString(mContentId));
		int id = cursor.getInt(mId);;
		holder.id.setText(id + "");
		final Integer indext = new Integer(id);
		if ( ListNoteUseSimpleCursorAdapter.arrayChecked.contains(indext) ) {
			holder.check.setChecked(true);
		} else {
			holder.check.setChecked(false);
		}
		
		holder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if ( buttonView.isChecked() ) {
//					Toast.makeText(mContext, "checked", Toast.LENGTH_SHORT).show();
					ListNoteUseSimpleCursorAdapter.arrayChecked.add(indext);
//					Toast.makeText(mContext, "size of array: " + ListNoteUseSimpleCursorAdapter.arrayChecked.size(), Toast.LENGTH_SHORT).show();
				} else {
//					Toast.makeText(mContext, "unchecked", Toast.LENGTH_SHORT).show();
					ListNoteUseSimpleCursorAdapter.arrayChecked.remove(indext);
//					Toast.makeText(mContext, "size of array: " + ListNoteUseSimpleCursorAdapter.arrayChecked.size(), Toast.LENGTH_SHORT).show();
				}
				ListNoteUseSimpleCursorAdapter.havecheck();
			}
		});
	}
}
